package com.example.sunming.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;

import java.util.List;
import java.util.Set;

/**
 * @description: easyexcel工具类
 * @date: 2021/1/12
 * @author: sunming
 */
public class EasyExcelUtil {

    /**
     * 最简单的写
     *
     * @param fileName  文件名，可以加上全路径
     * @param sheetName sheet名
     * @param dataList  数据
     * @param tClass
     */
    public static void simpleWrite(String fileName, String sheetName, List<?> dataList, Class<?> tClass) {
        ExcelWriter excelWriter = null;
        try {
            excelWriter = EasyExcel.write(fileName, tClass).build();
            WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).build();
            excelWriter.write(dataList, writeSheet);
        } finally {
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }

    /**
     * 写出需要的列
     *
     * @param fileName  文件名，可以加上全路径
     * @param sheetName sheet名
     * @param dataList  数据
     * @param tClass
     */
    public static void includeWrite(String fileName, String sheetName, List<?> dataList, Class<?> tClass, Set<String> includeColumnFileNames) {
        EasyExcel.write(fileName, tClass).includeColumnFiledNames(includeColumnFileNames).sheet(sheetName).doWrite(dataList);
    }

    /**
     * 写出排除需要的列
     *
     * @param fileName  文件名，可以加上全路径
     * @param sheetName sheet名
     * @param dataList  数据
     * @param tClass
     */
    public static void excludeWrite(String fileName, String sheetName, List<?> dataList, Class<?> tClass, Set<String> excludeColumnFileNames) {
        EasyExcel.write(fileName, tClass).excludeColumnFiledNames(excludeColumnFileNames).sheet(sheetName).doWrite(dataList);
    }

}
